# -*- coding:UTF-8 -*-
import requests
from bs4 import BeautifulSoup
import os
import requests
import threading
from threadUtils import pool


def figure_handle(soup: BeautifulSoup) -> {}:
    """
    :param soup:
    :return:
    """
    result = {}
    thumb_url = soup.img['data-src']
    result['thumb_url'] = thumb_url  # 缩略图路径
    result['wight'] = int(soup.div.span.string.strip(' ').replace(' ', '').split('x')[0])
    result['height'] = int(soup.div.span.string.strip(' ').replace(' ', '').split('x')[1])
    result['format'] = "jpg"
    if soup.find('span', class_="png"):
        result['format'] = "png"
    name = thumb_url[thumb_url.rfind('/') + 1:thumb_url.rfind('.')]
    result['source_url'] = thumb_url.replace('th.wallhaven.cc/small/', 'w.wallhaven.cc/full/').replace(name,
                                                                                                       'wallhaven-' + name).replace(
        'jpg', result['format'])
    return result


def getPage(page: int) -> []:
    target = f'https://wallhaven.cc/search?q=&page={page}'
    req = requests.get(url=target)
    # print(req.text)

    soup = BeautifulSoup(req.text, 'html.parser')
    ## 格式化网页内容
    # print(soup.prettify())
    ## 查找元素种类为figure的项
    # print(soup.findAll('figure'))
    # print(soup.findAll('figure')[0])
    figure_list = soup.findAll('figure')
    result = []
    for fs in figure_list:
        # print(fs.prettify())
        # print(fs.img['data-src'])
        # print(fs.div.span.string.strip(' ').replace(' ', '').split('x'))
        # print(figure_handle(fs))
        result.append(figure_handle(fs))
    return result


def download_file(url, store_path):
    filename = url.split('/')[-1]
    filepath = os.path.join(store_path, filename)
    if os.path.exists(filepath):
        return
    file_data = requests.get(url, allow_redirects=True).content
    with open(filepath, 'wb') as handler:
        print(f'下载{url}')
        handler.write(file_data)


# def download_thread(url,store_path):
#     t = threading.Thread(target=download_thread,args=(url,store_path))
#     t.setDaemon(True)
#     return t

if __name__ == '__main__':
    store_path = 'D://tmp'
    if not os.path.exists(store_path):
        os.makedirs(store_path)
    for i in range(1, 10):
        list = getPage(i)
        for image in list:
            pool.put(download_file, (image['source_url'], store_path))

#
# https://wallhaven.cc/w/g8d9ll
# https://w.wallhaven.cc/full/g8/wallhaven-g8d9ll.png
#
# https://wallhaven.cc/w/n61xg7
# https://w.wallhaven.cc/full/n6/wallhaven-n61xg7.png
